package org.ats.http;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.util.EntityUtils;
import org.ats.utils.HttpHelper;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

/**
 * 未测试
 * */
public class DataHttp extends AtsHttp {

	private String TAG = "DataHttp";
	Context mCm;
	final String murl;
	private CallBackDataListener mCallBackDataListener;
	private final int mActionId;
	private Exception mException;
	private SharedPreferences sp;

	/**
	 * @param context
	 *            上下文{@link Context}
	 * @param url
	 *            地址 {@link String}
	 * @param actionId
	 *            -本次联网指定ID {@link Integer}
	 * */
	public DataHttp(Context context, String url, int actionId) {
		mCm = context;
		murl = url;
		mActionId = actionId;
		sp = PreferenceManager.getDefaultSharedPreferences(mCm);
	}

	@Override
	public void run() {
		running = true;
		while (running) {
			networkExchange();
		}
	}

	@Override
	void setHeader(final HttpRequestBase hrb) {

	}

	void networkExchange() {
		final int MAX_COUNT = 3;
		int HTTP_COUNT = 0;
		try {
			String mdata = null;
			final HttpClient httpClient = HttpHelper.getHttpClient(mCm
					.getApplicationContext());
			HttpGet mGet = new HttpGet(murl);
			setHeader(mGet);
			HTTP_COUNT++;
			final HttpResponse resp = httpClient.execute(mGet);
			final HttpEntity entity = resp.getEntity();

			final int statusCode = resp.getStatusLine().getStatusCode();
			if (statusCode == HttpStatus.SC_OK || entity != null) {
				// final byte[] respBytes = EntityUtils.toByteArray(entity);
				final String mStr = EntityUtils.toString(entity);
				mdata = mStr;
				if (mStr != null) {
					updataRx(mStr.getBytes().length);
				}
			}
			PostCallBackData(mdata);

		} catch (Exception ex) {
			ex.printStackTrace();
			System.gc();
			mException = ex;
			if (HTTP_COUNT > MAX_COUNT) {
				running = false;
				PostCallBackException(ex);
			}
		} finally {
			if (mException != null)
				running = false;
		}
	}

	/**
	 * 接收数据
	 * */
	void updataRx(int v) {
		long n = sp.getLong("dh-rx", 0);
		sp.edit().putLong("dh-rx", n + v).commit();
	}

	/**
	 * 发送数据
	 * */
	void updataTx(int v) {
		long n = sp.getLong("dh-tx", 0);
		sp.edit().putLong("dh-tx", n + v).commit();
	}

	@Override
	public long getRx(int v) {
		return 0;
	}

	@Override
	public long getTx(int v) {
		return 0;
	}

	private void PostCallBackData(final String data) {
		if (mCallBackDataListener != null) {
			mCallBackDataListener.onPostExecute(mActionId, data);
		}
	}

	private void PostCallBackException(Exception ex) {
		if (mCallBackDataListener != null)
			mCallBackDataListener.onError(mActionId, ex);

	}

	public void setCallBackDataListener(CallBackDataListener Listener) {
		mCallBackDataListener = Listener;
	}

}
